<?php

/**
 * MMOA MYSQL 公共类库
 * ============================================================================
 * * 版权所有 2012 铭迈（上海）实业有限公司，并保留所有权利。
 * 网站地址: http://www.mingmailtd.com/；
 * ----------------------------------------------------------------------------
 * 仅用于公司内部人员使用。
 * ============================================================================
 * $Author: zhangmingming $
 * $Id: lib_smart.php 2012/6/26 15:08:40 $
 */

if(!defined('IN_MMOA')){
	die('Hacking attempt');
}

class mm_mysql
{
	var $link_id    = NULL;
	
	var $settings   = array();
	
	var $root_path	= '';
	
	function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf8', $pconnect = 0, $quiet = 0)
	{
		$this->mm_mysql($dbhost, $dbuser, $dbpw, $dbname, $charset, $pconnect, $quiet);
	}
	
	function mm_mysql($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf8', $pconnect = 0, $quiet = 0)
	{
		if (defined('MM_CHARSET'))
		{
			$charset = strtolower(str_replace('-', '', EC_CHARSET));
		}
		
		if (defined('ROOT_PATH') && !$this->root_path)
		{
			$this->root_path = ROOT_PATH;
		}
		
		if ($quiet)
		{
			$this->connect($dbhost, $dbuser, $dbpw, $dbname, $charset, $pconnect, $quiet);
		}
		else
		{
			$this->settings = array(
					'dbhost'   => $dbhost,
					'dbuser'   => $dbuser,
					'dbpw'     => $dbpw,
					'dbname'   => $dbname,
					'charset'  => $charset,
					'pconnect' => $pconnect
			);
		}
	}
	
	function connect($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf8', $pconnect = 0, $quiet = 0)
	{
		if ($pconnect)
		{
			if (!($this->link_id = @mysql_pconnect($dbhost, $dbuser, $dbpw)))
			{
				if (!$quiet)
				{
					$this->ErrorMsg("Can't pConnect MySQL Server($dbhost)!");
				}
		
				return false;
			}
		}
		else
		{
			if (PHP_VERSION >= '4.2')
			{
				$this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw, true);
			}
			else
			{
				$this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw);
		
				mt_srand((double)microtime() * 1000000); // 对 PHP 4.2 以下的版本进行随机数函数的初始化工作
			}
			
			if (!$this->link_id)
			{
				if (!$quiet)
				{
					$this->ErrorMsg("Can't Connect MySQL Server($dbhost)!");
				}
		
				return false;
			}
		}
	}
	
	
}